/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 16, 2012  2:42:50 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : sap.business.mm.inbound
 * File Name : MmVendorMasInbService.java
 * TODO
 */
package sap.business.mm.inbound;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;

import jpa.entitys.synchro.common.ACTION_HANDLER;
import jpa.entitys.synchro.staging.MmVendorMasInb;
import jpa.service.common.SessionEntityManager;
import jpa.service.synchro.staging.MmVendorMasInbServices;

import org.apache.log4j.Logger;

import com.sap.mw.jco.JCO;

import promis.utility.CommonService;
import sap.business.commons.services.CommonJCOReturn;
import apps.promis.in.common.SupplierMasterAction;
/**
 * @author Chatree Siangklom of Administrator
 * Date =Nov 12, 2013 : 2:06:10 PM 
 * CreateBy=Chatree Siangklom
 * 
 * MmVendorMasInbService
 * TODO
 */
public class MmVendorMasInbService {
	
	private static final Logger log = Logger.getLogger(MmVendorMasInbService.class);
	
	/**
	 * Date =Nov 12, 2013 : 2:06:18 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @param input
	 * @param tables
	 * @param output
	 * @param structureOrTableName
	 */
	public void CreatedSync(JCO.ParameterList input, JCO.ParameterList tables, JCO.ParameterList output, String structureOrTableName)
	{
		log.info("Start CreatedSync  --->");
		
		List<MmVendorMasInb> lst_object = new ArrayList<MmVendorMasInb>();
		JCO.Table table = tables.getTable(structureOrTableName);
		List<MmVendorMasInb> lst = null;
		int records = -1;
		try{
			
			records = table.getNumRows();
			
			EntityManager em = new SessionEntityManager().CreatedEntityManager();
			MmVendorMasInbServices mmVendorMasInbServ = new MmVendorMasInbServices(em); 
			String instance_no = mmVendorMasInbServ.GetNewInstanceNo();
			
			for(int i = 0; i < records; i++)
			{
				log.info("i ="+ i);
				table.setRow(i);
				MmVendorMasInb model = new MmVendorMasInb();
				
				//model.setPromisStagingId(CommonService.IsNullRNull(table.getString("PROMIS_STAGING_ID")));
				model.setInstanceNo(instance_no);
				model.setSupplierCode(CommonService.IsNullRNull(table.getString("SUPPLIER_CODE")));
				model.setSupplierNameEnglish(CommonService.IsNullRNull(table.getString("SUPPLIER_NAME_ENGLISH")));
				model.setSupplierNameThai(CommonService.IsNullRNull(table.getString("SUPPLIER_NAME_THAI")));
				
				model.setAddressText(CommonService.IsNullRNull(table.getString("ADDRESS_TEXT")));				
				model.setCity(CommonService.IsNullRNull(table.getString("CITY")));
				model.setTumbon(CommonService.IsNullRNull(table.getString("TUMBON_CODE")));
				model.setZip(CommonService.IsNullRNull(table.getString("ZIP")));
				model.setProvince(CommonService.IsNullRNull(table.getString("PROVINCE_TEXT")));
				model.setPhone(CommonService.IsNullRNull(table.getString("PHONE")));
				model.setFax(CommonService.IsNullRNull(table.getString("FAX")));
				model.setSupplierGroup(CommonService.IsNullRNull(table.getString("SUPPLIER_GROUP")));
				model.setLocalImport(CommonService.IsNullRNull(table.getString("LOCAL_IMPORT")));
				model.setTypeOfSupplier(CommonService.IsNullRNull(table.getString("TYPE_OF_SUPPLIER")));
				model.setStatus(CommonService.IsNullRNull(table.getString("STATUS")));
				
				//Set Txn Reacord
				model.setCreatedOn(CommonService.IsNullRNull(table.getString("CREATED_ON")));
				model.setCreatedBy(CommonService.IsNullRNull(table.getString("CREATED_BY")));
				model.setUpdatedOn(CommonService.IsNullRNull(table.getString("LAST_UPD_ON")));
				model.setUpdatedBy(CommonService.IsNullRNull(table.getString("LAST_UPD_BY")));
				
				if(CommonService.IsNull(table.getString("LAST_UPD_ON")))
					model.setActionHandle(ACTION_HANDLER.INSERTED.toString());
				else 
					model.setActionHandle(ACTION_HANDLER.UPDATED.toString());
				
				//Set SAP External Key link
				model.setExtRefKey(CommonService.IsNullRNull(table.getString("SYNC_EXTERNAL_REF_KEY")));
				model.setExtRefNo(CommonService.IsNullRNull(table.getString("SYNC_EXTERNAL_REF_NO")));
				
				log.info(model.toString());
				lst_object.add(model);				
			}
			
			//Create To Staging
			mmVendorMasInbServ.Inserted(lst_object);
			log.info("Create To Staging..............>");
			
			//find By Instance
			lst = mmVendorMasInbServ.findByInstanceNo(instance_no);
			log.info("find By Instance No : "+ instance_no +" ..............>");
			
			if(!CommonService.IsNull(lst))
			{
				//Create to Promis
				new SupplierMasterAction().CreateToPromis(lst);
				log.info("Create to Promis");
				
				if(lst.size() == records){
					for(int i = 0; i < records; i++)
					{
						MmVendorMasInb obj = lst.get(i);
						table.setRow(i);
						table.setValue(CommonService.IsNullRString(obj.getPromisStagingId()), "PROMIS_TXN_ID");
						table.setValue("S", "MSG_CODE");
						table.setValue("Success", "MSG_DESC");
						table.setValue("SAP NO =" + obj.getExtRefNo(), "MSG_ALT_DESC");
						table.setValue("", "MSG_ACTION_RESULT");	
						log.info(CommonJCOReturn.toString(table));
					}
				}
			}else
				log.info("find not found  instance no = " + instance_no);
		}
		catch(Exception e)
		{	log.error(e);
			if(!CommonService.IsNull(lst)){
				//Create to Promis
					if(lst.size() == records){
						for(int i = 0; i < records; i++)
						{
							MmVendorMasInb obj = lst.get(i);
							table.setRow(i);
							table.setValue(CommonService.IsNullRString(obj.getPromisStagingId()), "PROMIS_TXN_ID");
							table.setValue("E", "MSG_CODE");
							table.setValue(e.getMessage(), "MSG_DESC");
							table.setValue("SAP NO =" + obj.getExtRefNo(), "MSG_ALT_DESC");
							table.setValue("", "MSG_ACTION_RESULT");
							
							try {
								log.info(CommonJCOReturn.toString(table));
							} catch (Exception e1) {
								log.error(e1.getMessage());
								System.err.println(e1.getMessage());
							}
						}
					}			
			}else{			
				for(int i = 0; i < records; i++)
				{
					table.setRow(i);
					table.setValue("E", "MSG_CODE");
					table.setValue(e.getMessage(), "MSG_DESC");
				}
			}
		}
	}
}
